<?php

namespace platform\core;

use cm;
use common\services\Authorization;
use yii\web\Controller;
use yii;

class BaseController extends Controller
{
    public $layout = "layout";
    public function beforeAction($action)
    {
        $flag = $this->auth();//权限验证
        return $flag;
    }

    private function auth(){
        $authService = new Authorization();
        $docAuth = $authService->docPermissions();
        $flag = false;
        switch ($docAuth['code']) {
            case 0://未登录
                Cm::$res->redirect(['main/login']);
                exit;
                break;
            case -1://模块停用
                break;
            case -2://操作停用
                break;
            case -3://角色权限验证
                $account = Cm::$app->user->getIdentity();
                $flag = $authService->rolePermissions($account['id']);
                break;
            default:
                $flag = true;
                break;
        }
        if (!$flag) {
            if (Yii::$app->request->isAjax)
                Cm::$res->error('没有权限!');
            else
                Cm::$res->redirect(['main/error','code'=>401,'msg'=>'没有权限']);
        }
        return $flag;
    }

}
